package com.jxl.domain.handler.subject;

import com.jxl.common.enums.DeletedCodeEnum;
import com.jxl.common.enums.SubjectTypeEnum;
import com.jxl.domain.convert.SubjectAnswerConvert;
import com.jxl.domain.entity.SubjectAnswerBO;
import com.jxl.domain.entity.SubjectInfoBO;
import com.jxl.domain.entity.SubjectOptionBO;
import com.jxl.infra.basic.entity.SubjectJudge;
import com.jxl.infra.basic.entity.SubjectMultiple;
import com.jxl.infra.basic.service.SubjectJudgeService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 判断策略
 */
@Component
public class JudgeHandler implements SubjectTypeHandler{
    @Resource
    private SubjectJudgeService subjectJudgeService;

    @Override
    public SubjectTypeEnum getHandlerType() {
        return SubjectTypeEnum.JUDGE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        List<SubjectAnswerBO> optionList = subjectInfoBO.getOptionList();
        if (optionList==null || optionList.size()==0){
            return;
        }
        SubjectAnswerBO subjectAnswerBO = optionList.get(0);
        SubjectJudge subjectJudge =
                SubjectAnswerConvert.INSTANCE.convertBOToSubjectJudge(subjectAnswerBO);
        subjectJudge.setIsDeleted(DeletedCodeEnum.UN_DELETE.getCode());
        subjectJudge.setSubjectId(subjectInfoBO.getSubjectId());
        subjectJudgeService.insert(subjectJudge);
    }

    @Override
    public SubjectOptionBO query(Long id) {
        SubjectJudge subjectJudge = new SubjectJudge();
        subjectJudge.setSubjectId(id);
        subjectJudge.setIsDeleted(DeletedCodeEnum.UN_DELETE.getCode());
        List<SubjectJudge> query = subjectJudgeService.query(subjectJudge);
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        List<SubjectAnswerBO> list = new ArrayList<>();
        if (query!=null && query.size()!=0){
            query.forEach(subjectJudge1->{
                SubjectAnswerBO subjectAnswerBO = new SubjectAnswerBO();
                subjectAnswerBO.setIsCorrect(subjectJudge1.getIsCorrect());
                list.add(subjectAnswerBO);
            });
            subjectOptionBO.setOptionList(list);
        }
        return subjectOptionBO;
    }
}
